# encoding=utf8
#
import pymysql,requests,urllib3,datetime,re
from lxml import etree
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

host = '127.0.0.1'
users = 'root'
pwd = '123123'
port = 3306
dbs = 'ans'


def mysql(id,新闻标题,热度,新闻链接,创建时间):
    value = ((id,新闻标题,热度,新闻链接,创建时间))
    db = pymysql.connect(host=host, user=users,passwd=pwd,port=port, db=dbs)
    cursor = db.cursor()
    sql = "INSERT INTO 今日头条(id,新闻标题,热度,新闻链接,创建时间) values(%s,%s,%s,%s,%s)"
    try:
        cursor.execute(sql, value)
        db.commit()
        print('success!')
    except Exception as e:
        db.rollback()
        print("error.", e)
    db.close()

def join_str(sth):
    str = ''.join(sth).strip()
    return str

def down(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Mobile Safari/537.36 Edg/103.0.1264.37',
        'Cookie':'__ac_nonce=062b88429003a98705c41; msToken=kpPasTtpL66AVDYS4SGtLOx45tLUZkmX3HzeeZh8GhG_G18tEQuHVBT4zPyPT6Tf7Kx4_XlnRxPZkCq7sMAbF2iv5wrBrxt7Lr9Nou2ELCc9; __ac_signature=_02B4Z6wo00f015-KraAAAIDDH4hX4H095tefqqkAAIVi34; tt_webid=7105677063995246087; ttcid=7efd93ec14ac4307b113da0a7b5d5ffc38; local_city_cache=%E9%83%91%E5%B7%9E; csrftoken=7c185c6beed9fe2b823f5f075daaa648; _tea_utm_cache_24=undefined; ttwid=1%7CwyP_do6sR3svCFEVpZiaSYMdkjucj6U8171zM2_idW8%7C1656259628%7Ce1991d22649479f8e44afb9a5a1365aa220a811df8e933ddad53f2067fa14bd8; MONITOR_WEB_ID=06e34afb-d2ef-4ad9-85c3-fe58d188ac2e; tt_scid=J9gMb7zMDXtaapd8bFcONDvLPCR.Sp2aYn9-5wgcvSvhX1nxcKfDBQO9rN2S4XeRb6f4; s_v_web_id=verify_l4vi7x2f_2Zzab6rA_1rUn_4CmU_8XSm_hdI2how0Ds4M',

    }
    res = requests.get(url=url,headers=headers).json()

    return res

def func(res):
    # print(res)
    data_list = res['data']
    print(data_list)
    for data in data_list:
        id = data['ClusterId']
        新闻标题 = join_str(data['Title'])
        新闻链接 = join_str(data['Url'])
        热度 = eval(data['HotValue'])
        print(id,新闻标题,热度,新闻链接,datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S'))
        mysql(id,新闻标题,热度,新闻链接,datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S'))
        # print(academeId,dwmc,forumId,ssdm,city,zgbm)
        # mysql(academeId,dwmc,forumId,ssdm,city,zgbm)
        # try:

        # print(user, user_url, title,content,post_time,think,reply,like,dislike)
            # break



    return 0
if __name__ == "__main__":
    # for page in range(0,900,20):
    #     print('page',page)
        url = 'https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc'
        # print(url)
        res = down(url)
        # print(res)
        func(res)
        # break

